package demo.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import demo.dao.ReaderDao;
import demo.entity.Reader;
import demo.util.SwingUtil;

public class ReaderRegister extends JFrame implements ActionListener{
	private JFrame frame = new JFrame("图书馆管理系统 ");
	private JPanel ReaderRegisterPane;
	private ImageIcon background;
	private JTextField Name;
    private JTextField Number;
    private JPasswordField Password;
    private JPasswordField PasswordAgain;
    String name0,sex0,telephone0,email0,password0,passwordAgain0;
    int number0,age0;
    private JTextField Sex;
    private JTextField Age;
    private JTextField Telephone;
    private JTextField Email;
    private JTextField Pro;
    private JTextField Grade;
    private Reader reader = new Reader();
    private ReaderDao readerDao = new ReaderDao();
    

	public ReaderRegister() {
		 background = new ImageIcon(this.getClass().getResource("background4.jpg"));
		 JLabel label = new JLabel(background);
		 label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		
		 ReaderRegisterPane = (JPanel) frame.getContentPane(); 
		 //getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		 ReaderRegisterPane.setOpaque(false);
		 ReaderRegisterPane.setLayout(null);
		 frame.getLayeredPane().setLayout(null);
		 
		 frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); 

		
		
	    /*ReaderRegisterPane = (JPanel) frame.getContentPane();  
	    ReaderRegisterPane.setLayout(null);*/
	    
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setBounds(450, 200, 600, 530);
	    frame.setResizable(false);
	    frame.setVisible(true);
	    SwingUtil.setWindowCenter(frame);
	    
	    JLabel name = new JLabel("用户名:");
	    ReaderRegisterPane.add(name);
	    name.setBounds(82, 24, 100, 20);
	    name.setFont(new Font("",Font.BOLD,20));
			
	    Name = new JTextField();
	    ReaderRegisterPane.add(Name);
	    Name.setBounds(190, 21, 155, 25);
	    Name.setColumns(10);
	
	    JLabel number = new JLabel("学号:");
	    ReaderRegisterPane.add(number);
	    number.setBounds(82, 64, 100, 20);
	    number.setFont(new Font("",Font.BOLD,20));

	    Number = new JTextField();
	    ReaderRegisterPane.add(Number);
	    Number.setColumns(10);
	    Number.setBounds(190, 61, 155, 25);
	    
	    JLabel sex = new JLabel("性别:");
	    sex.setBounds(82, 100, 100, 20);
	    sex.setFont(new Font("",Font.BOLD,20));
	    frame.getContentPane().add(sex);
	    
	    Sex = new JTextField();
	    Sex.setBounds(190, 97, 155, 25);
	    frame.getContentPane().add(Sex);
	    Sex.setColumns(10);
		    
	    JLabel age = new JLabel("年龄:");
	    age.setBounds(82, 140, 100, 20);
	    age.setFont(new Font("",Font.BOLD,20));
	    frame.getContentPane().add(age);
	
	    Age = new JTextField();
	    Age.setColumns(10);
	    Age.setBounds(190, 137, 155, 25);
	    frame.getContentPane().add(Age);
	    	
	    JLabel telephone = new JLabel("电话号:");
	    telephone.setBounds(82, 180, 100, 20);
	    telephone.setFont(new Font("",Font.BOLD,20));
        frame.getContentPane().add(telephone);
	
	    Telephone = new JTextField();
	    Telephone.setColumns(10);
	    Telephone.setBounds(190, 177, 155, 25);
	    frame.getContentPane().add(Telephone);
	
	    JLabel email = new JLabel("邮箱:");
	    email.setBounds(82, 220, 100, 20);
	    email.setFont(new Font("",Font.BOLD,20));
	    frame.getContentPane().add(email);
	
	    Email = new JTextField();
	    Email.setColumns(10);
	    Email.setBounds(190, 217, 155, 25);
	    frame.getContentPane().add(Email);
	   
	    JLabel pro =new JLabel("专业:");
	    pro.setBounds(82, 260, 100, 20);
	    pro.setFont(new Font("",Font.BOLD,20));
	    frame.getContentPane().add(pro);
	    
	    Pro = new JTextField();
	    Pro.setColumns(10);
	    Pro.setBounds(190,257,155,25);
	    frame.getContentPane().add(Pro);
	    
	    JLabel grade=new JLabel("年级:");
	    grade.setBounds(82, 300, 100, 20);
	    grade.setFont(new Font("",Font.BOLD,20));
	    frame.getContentPane().add(grade);
	    
	    Grade = new JTextField();
	    Grade.setColumns(10);
	    Grade.setBounds(190,297,155,25);
	    frame.getContentPane().add(Grade);
	    
	    JLabel password = new JLabel("密码:");
	    ReaderRegisterPane.add(password);
	    password.setBounds(82, 340, 100, 20);
	    password.setFont(new Font("",Font.BOLD,20));
	
	    Password = new JPasswordField();
	    ReaderRegisterPane.add(Password);
	    Password.setColumns(10);
	    Password.setBounds(190, 337, 155, 25);
	
	
	    JLabel passwordAgain = new JLabel("确认密码:");
	    ReaderRegisterPane.add(passwordAgain);
	    passwordAgain.setBounds(82, 380, 100, 20);
	    passwordAgain.setFont(new Font("",Font.BOLD,20));
	
	    PasswordAgain = new JPasswordField();
	    ReaderRegisterPane.add(PasswordAgain);
	    PasswordAgain.setColumns(10);
	    PasswordAgain.setBounds(190, 377, 155, 25);
	
	    JButton register = new JButton("注册");
	    frame.getContentPane().add(register);
	    register.setBounds(77, 418, 100, 30);
	    register.addActionListener(this);
	    register.setFont(new Font("",Font.BOLD,17));
	
	
	    JButton cancle = new JButton("取消");
	    frame.getContentPane().add(cancle);
	    cancle.setBounds(255, 418, 100, 30);
	    cancle.setFont(new Font("",Font.BOLD,17));

	    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    
	    register.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		try {
	    			String id0= Number.getText();
	    			String name0= Name.getText();
	    			String password0= new String(Password.getPassword());
	    			String sex0= Sex.getText();
	    			Date birth0=Date.valueOf(Age.getText());
	    			String pro0=Pro.getText();
	    			String grade0=Grade.getText();
	    			String tel0=Telephone.getText();
	    			String email0=Email.getText();
	    			String passwordAgain0 = new String(PasswordAgain.getPassword());
	    			
	    			if(!password0.equals(passwordAgain0))
			    	{				    		
			    		//您的密码不一致				    		
			    		JOptionPane.showMessageDialog(null,"您的密码不一致，请重新输入","错误",JOptionPane.INFORMATION_MESSAGE);
			    		Password.setText("");
			    		PasswordAgain.setText("");
			    	}
			    	else
			    	{
			    		if(Name.getText().length()>4)
			    		{
			    		  JOptionPane.showMessageDialog(null,"姓名输入错误","错误",JOptionPane.INFORMATION_MESSAGE);
			    		  Name.setText("");
			    		  }
			    		  else  if(Number.getText().length()!=12)
			    		  {
			    			JOptionPane.showMessageDialog(null,"学号输入错误","错误",JOptionPane.INFORMATION_MESSAGE);
			    			Number.setText("");
			    			}
			    		  else	if(!Sex.getText().equals("男")&&!Sex.getText().equals("女"))
			    		    {
			    		      JOptionPane.showMessageDialog(null,"性别输入错误","错误",JOptionPane.INFORMATION_MESSAGE);
			    		      Sex.setText("");
			    		      }
			    		 /* else  if(Age.getText().length()!=2)
			    		      {
			    			    JOptionPane.showMessageDialog(null,"年龄输入错误","错误",JOptionPane.INFORMATION_MESSAGE);
			    			    Age.setText("");
			    			    }*/
			    		  else if(Telephone.getText().length()!=11)
					    		{
					    			JOptionPane.showMessageDialog(null,"电话号输入错误","错误",JOptionPane.INFORMATION_MESSAGE);
					    			Telephone.setText("");
					    			}
			    	//}
	    			
			    		  else {
	    			reader.setId(id0);
	    			reader.setName(name0);
	    			reader.setPassword(password0);
	    			reader.setSex(sex0);
	    			reader.setBirth(birth0);
	    			reader.setPro(pro0);
	    			reader.setGrade(grade0);
	    			reader.setTel(tel0);
	    			reader.setEmail(email0);
	    			
	    			int affectedRecords = readerDao.save(reader);
	    			if (affectedRecords == 1) {// 如果保存成功
	    				JOptionPane.showMessageDialog(null, "你插入的记录内容为：" + readerDao.findById(id0).toString(),
	    						"插入" + affectedRecords + "条记录成功！", JOptionPane.INFORMATION_MESSAGE);
	    				//清空输入框，以备插入下一条记录
	    				Number.setText("");
	    				Name.setText("");
	    				Password.setText("");
	    				Sex.setText("");
	    				Age.setText("");
	    				Pro.setText("");
	    				Grade.setText("");
	    				Telephone.setText("");
	    				Email.setText("");
	    				PasswordAgain.setText("");
	    				//Number.requestFocus();

	    			} else if (affectedRecords == 0) {// 如果记录已存在
	    				JOptionPane.showMessageDialog(null, "记录已经存在！", "插入失败",JOptionPane.ERROR_MESSAGE);
	    				Number.setText("");
	    				Name.setText("");
	    				Password.setText("");
	    				Sex.setText("");
	    				Age.setText("");
	    				Pro.setText("");
	    				Grade.setText("");
	    				Telephone.setText("");
	    				Email.setText("");
	    				PasswordAgain.setText("");

	    			} else {// 保存时发生异常
	    				JOptionPane.showMessageDialog(null, "保存记录出错，可能是数据库有问题！", "插入失败", JOptionPane.ERROR_MESSAGE);
	    			}
	    			}
			    	}
			    	}catch (Exception e1) {
	    			JOptionPane.showMessageDialog(null, "数据格式有误！", "插入失败", JOptionPane.ERROR_MESSAGE);
	    		
			   }
	    	}
	    });
	    cancle.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		frame.dispose();
	    	}
	    	});
	    
}
	


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		
	}

}
